package su.jet.common.irc.protocol.request;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

import su.jet.common.irc.domain.User;
import su.jet.common.irc.protocol.Answer;
import su.jet.common.irc.protocol.Session;
import su.jet.irc.server.service.AuthService;
import su.jet.irc.server.service.DataService;

@SuppressWarnings("serial")
public class AuthRequest implements Request {

	private User user;

	public AuthRequest(User user) {
		this.user = user;
	}

	public User getUser() {
		return user;
	}


	@Override
	public void process(Session session, Set<User> regUsers, Map<User, Session> onlineUsers, DataService dataService) throws IOException {
		User sender = this.getUser();
				
		Boolean authResult = AuthService.getAuthResult(regUsers, sender);
		
		Answer serverAnswer = new Answer(this, authResult);
		session.sendAnswer(serverAnswer);
		
		if (authResult){ 
			onlineUsers.put(sender, session);
		}
		
	}
}
